package com.example.fourm.controller;

import ch.qos.logback.classic.Logger;
import com.example.fourm.common.AppResult;
import com.example.fourm.common.ResultCode;
import com.example.fourm.config.AppConfig;
import com.example.fourm.exception.ApplicationException;
import com.example.fourm.model.Article;
import com.example.fourm.model.ArticleReply;
import com.example.fourm.model.User;
import com.example.fourm.services.IArticleReplyService;
import com.example.fourm.services.IArticleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;
    @RequestMapping("/create")
    public AppResult create(@RequestParam("articleId") @NonNull Long articleId,
                            @RequestParam("content") @NonNull String content, HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        //先判断用户是否有权限访问
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user == null || user.getDeleteState() != 0) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.getMessage()+" 用户不存在！");
            throw new ApplicationException(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
        }
        if(user.getState() != 0){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage()+" 用户已被禁言！");
            throw new ApplicationException(ResultCode.FAILED_USER_BANNED.getMessage());
        }
        Article article = articleService.selectById(articleId);
        if (article == null || article.getState() != 0) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReplyService.create(articleReply);
        return AppResult.success("回复帖子成功！！");
    }

    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@RequestParam("articleId") @NonNull Long articleId,
                                                               HttpServletRequest request) {
        Article article = articleService.selectById(articleId);
        if (article == null || article.getState() != 0) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_NOT_EXISTS));
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
